﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCCN.DAL;

namespace SCCN.BLL
{
    public class AdvertisementBLL
    {
        private static AdvertisementBLL instance = null;

        /// <summary>
        /// 单例构造
        /// </summary>
        /// <returns></returns>
        public static AdvertisementBLL getInstance()
        {
            if (instance == null)
            {
                instance = new AdvertisementBLL();
            }
            return instance;
        }

        public bool Add(SC_Advertisement obj)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                entites.AddToSC_Advertisement(obj);
                return entites.SaveChanges() > 0 ? true : false;
            }
        }

        public bool Update(SC_Advertisement obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_Advertisement objSC_Advertisement = entities.SC_Advertisement.FirstOrDefault(v => v.Id == obj.Id);
                entities.Detach(objSC_Advertisement);
                objSC_Advertisement.AdDesc = obj.AdDesc;
                objSC_Advertisement.AdName = obj.AdName;
                objSC_Advertisement.AdSpaceId = obj.AdSpaceId;
                objSC_Advertisement.IsPic = obj.IsPic;
                objSC_Advertisement.PicSrc = obj.PicSrc;
                objSC_Advertisement.IsDelete = obj.IsDelete;
                objSC_Advertisement.OrderId = obj.OrderId;
                objSC_Advertisement.TargetUrl = obj.TargetUrl;
                entities.Attach(objSC_Advertisement);
                var newSC_Advertisement = entities.ObjectStateManager.GetObjectStateEntry(objSC_Advertisement);
                newSC_Advertisement.SetModified();
                newSC_Advertisement.SetModifiedProperty("AdDesc");
                newSC_Advertisement.SetModifiedProperty("AdName");
                newSC_Advertisement.SetModifiedProperty("AdSpaceId");
                newSC_Advertisement.SetModifiedProperty("IsPic");
                newSC_Advertisement.SetModifiedProperty("PicSrc");
                newSC_Advertisement.SetModifiedProperty("OrderId");
                newSC_Advertisement.SetModifiedProperty("IsDelete");
                newSC_Advertisement.SetModifiedProperty("TargetUrl");
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public bool Delete(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_Advertisement obj = entities.SC_Advertisement.FirstOrDefault(r => r.Id == id);
                obj.IsDelete = true;
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public SC_Advertisement GetAdvertisementById(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Advertisement.FirstOrDefault(r => r.IsDelete == false && r.Id == id);
            }
        }

        /// <summary>
        /// 根据广告名查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public List<SC_Advertisement> GetAdvertisementsByName(string adName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Advertisement.Where(r => r.IsDelete == false && r.AdName.Contains(adName)).ToList<SC_Advertisement>();
            }
        }

        /// <summary>
        /// 根据广告名查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public SC_Advertisement GetAdvertisementByName(string adName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Advertisement.FirstOrDefault(r => r.IsDelete == false && r.AdName == adName);
            }
        }


        public List<SC_Advertisement> GetAdvertisementByAdSpaceId(int adSpacesId)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Advertisement.Where(r => r.IsDelete == false && r.AdSpaceId == adSpacesId).ToList<SC_Advertisement>();
            }
        }

        /// <summary>
        /// 根据条件查询广告位
        /// </summary>
        /// <returns></returns>
        public List<SC_Advertisement> GetAdvertisementByCondition(SC_Advertisement obj, int pageIndex, int pageSize, out int rowCount)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                rowCount = 0;
                var list = from r in entites.SC_Advertisement
                           select r;
                if (obj.AdName.Length > 0)
                {
                    list = list.Where(a => a.AdName.Contains(obj.AdName));
                }
                if (obj.AdSpaceId > 0)
                {
                    list = list.Where(a => a.AdSpaceId == obj.AdSpaceId);
                }
                if (obj.IsPic != null)
                {
                    list = list.Where(a => a.IsPic == obj.IsPic);
                }
                if (obj.IsDelete != null)
                {
                    list = list.Where(a => a.IsDelete == obj.IsDelete);
                }
                if (obj.PicSrc.Length > 0)
                {
                    list = list.Where(a => a.PicSrc == obj.PicSrc);
                }
                if (pageSize > 0)
                {
                    rowCount = list.Count<SC_Advertisement>();
                    list = list.OrderBy(a => a.OrderId).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                }
                return list.ToList<SC_Advertisement>();
            }
        }
    }
}
